/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipo.mm.web.formbeans;

import java.util.Locale;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;
import org.ipo.mm.ejb.entities.Institution;
import org.ipo.mm.ejb.entities.Representative;
import org.ipo.mm.web.util.ConstantManager;

/**
 *
 * @author makcro
 */
public class RepresentativeBean extends org.apache.struts.validator.ValidatorForm {

    private String firstName;
    private String lastName;
    private String fax;
    private String telephone;
    private String email;
    private String function;
    private String note;
    private String institution;
    private String operation;

    /**
     *
     */
    public RepresentativeBean() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param request The HTTP Request we are processing.
     * @return
     */
    @Override
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        MessageResources mResources = (MessageResources) request.getAttribute(Globals.MESSAGES_KEY);
        Locale locale = (Locale) request.getSession().getAttribute(Globals.LOCALE_KEY);

        String create = mResources.getMessage(locale, "gen_create");
        String update = mResources.getMessage(locale, "gen_update");

        ActionErrors errors = null;
        if (create.equals(operation) || update.equals(operation)) {
            errors = super.validate(mapping, request);
        }



        return errors;
    }

    /**
     * @return the firstName
     */
    public String getFirstName() {
        return firstName;
    }

    

    public String getInstitution() {
        return institution;
    }

    public void setInstitution(String institution) {
        this.institution = institution;
    }

    /**
     * @return the lastName
     */
    public String getLastName() {
        return lastName;
    }

    
    /**
     * @return the fax
     */
    public String getFax() {
        return fax;
    }

    /**
     * @param fax the fax to set
     */
    public void setFax(String fax) {
        this.fax = fax;
    }

    /**
     * @return the telephone
     */
    public String getTelephone() {
        return telephone;
    }

    /**
     * @param telephone the telephone to set
     */
    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return the function
     */
    public String getFunction() {
        return function;
    }

   

    /**
     * @return the note
     */
    public String getNote() {
        return note;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setFunction(String function) {
        this.function = function;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public void setNote(String note) {
        this.note = note;
    }

    

    /**
     * @return the operation
     */
    public String getOperation() {
        return operation;
    }

    /**
     * @param operation the operation to set
     */
    public void setOperation(String operation) {
        this.operation = operation;
    }

    @Override
    public void reset(ActionMapping mapping, HttpServletRequest request) {
        Boolean resetForm = (Boolean) request.getSession().getAttribute(ConstantManager.SESSION_REPR_RESET_FORM);

        Institution lastCreatedInstitution = (Institution) request.getAttribute(ConstantManager.REQUEST_INSTITUTION_LAST_CREATED);
        if(lastCreatedInstitution != null){
            this.institution = lastCreatedInstitution.getId().toString();
        }

        if (resetForm) {
            String action = request.getParameter("method");

            if ("showUpdateReprForm".equals(action)) {
                Representative repr = (Representative) request.getSession().getAttribute(ConstantManager.SESSION_REPRESENTATIVE_FOR_UPDATE_PN);

                this.setEmail(repr.getEmail() == null ? "" : repr.getEmail());
                this.setFax(repr.getFax() == null ? "" : repr.getFax());
                this.setFirstName(repr.getFirstName() == null ? "" : repr.getFirstName());
                this.setFunction(repr.getFunction() == null ? "" : repr.getFunction());
                this.setInstitution(repr.getInstitution() == null ? "" : String.valueOf(repr.getInstitution().getId()));
                this.setLastName(repr.getLastName() == null ? "" : repr.getLastName());
                this.setNote(repr.getNote() == null ? "" : repr.getNote());
                this.telephone = repr.getTelephone() == null ? "" : repr.getTelephone();

            } else if ("showCreateReprForm".equals(action)) {
                this.email = null;
                this.fax = null;
                this.firstName = null;
                this.function = null;
                this.institution = null;
                this.lastName = null;
                this.note = null;
                this.telephone = null;
            }
        }

    }
}
